<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('su.bankInfo.type')" prop="type">
        <el-select v-model="formData.type" :placeholder="t('common.inputText') + t('su.bankInfo.type')" @change="chooseType">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SU_BANK_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('su.bankInfo.compId')" prop="compId" v-if="formData.type">
        <el-select v-model="formData.compId" :placeholder="t('common.inputText') + t('su.bankInfo.compId')">
          <el-option
            v-for="cp in companys"
            :key="cp.id"
            :label="cp.name"
            :value="cp.id"
            @change="chooseType"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('su.bankInfo.bankName')" prop="bankName">
        <el-input v-model="formData.bankName" :placeholder="t('common.inputText') + t('su.bankInfo.bankName')" />
      </el-form-item>
      <el-form-item :label="t('su.bankInfo.swiftCode')" prop="swiftCode">
        <el-input v-model="formData.swiftCode" :placeholder="t('common.inputText') + t('su.bankInfo.swiftCode')" />
      </el-form-item>
      <el-form-item :label="t('su.bankInfo.bankCode')" prop="bankCode">
        <el-input v-model="formData.bankCode" :placeholder="t('common.inputText') + t('su.bankInfo.bankCode')" />
      </el-form-item>
      <el-form-item :label="t('su.bankInfo.branchCode')" prop="branchCode">
        <el-input v-model="formData.branchCode" :placeholder="t('common.inputText') + t('su.bankInfo.branchCode')" />
      </el-form-item>
      <el-form-item :label="t('su.bankInfo.bankAddress')" prop="bankAddress">
        <el-input v-model="formData.bankAddress" :placeholder="t('common.inputText') + t('su.bankInfo.bankAddress')" />
      </el-form-item>
      <el-form-item :label="t('su.bankInfo.acName')" prop="acName">
        <el-input v-model="formData.acName" :placeholder="t('common.inputText') + t('su.bankInfo.acName')" />
      </el-form-item>
      <el-form-item :label="t('su.bankInfo.account')" prop="account">
        <el-input v-model="formData.account" :placeholder="t('common.inputText') + t('su.bankInfo.account')" />
      </el-form-item>
      <el-form-item :label="t('su.bankInfo.remark')" prop="remark">
        <el-input v-model="formData.remark" :placeholder="t('common.inputText') + t('su.bankInfo.remark')" />
      </el-form-item>
      <el-form-item :label="t('su.bankInfo.pic')" prop="pic">
        <UploadImg v-model="formData.pic" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">{{ t('common.ok') }}</el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { BankInfoApi, BankInfoVO } from '@/api/su/bankinfo'
import {CommonApi} from "@/api/su/common";

/** 银行信息 表单 */
defineOptions({ name: 'BankInfoForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  type: undefined,
  compId: undefined,
  bankName: undefined,
  swiftCode: undefined,
  bankCode: undefined,
  branchCode: undefined,
  bankAddress: undefined,
  acName: undefined,
  account: undefined,
  defaultStatus: undefined,
  remark: undefined,
  pic: undefined
})
const formRules = reactive({
  type: [{ required: true, message: t('su.bankInfo.type')+t('validate.notempty'), trigger: 'change' }],
  compId: [{ required: true, message: t('su.bankInfo.compId')+t('validate.notempty'), trigger: 'change' }],
  bankName: [{ required: true, message: t('su.bankInfo.bankName')+t('validate.notempty'), trigger: 'blur' }],
  acName: [{ required: true, message: t('su.bankInfo.acName')+t('validate.notempty'), trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

const companys = ref([])

const chooseType = async () => {
  if ('Company' == formData.value.type) {
    companys.value = await CommonApi.getCompanyList()
  } else {
    companys.value = await CommonApi.getSupplierList()
  }
}

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await BankInfoApi.getBankInfo(id)
      chooseType()
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as BankInfoVO
    if (formType.value === 'create') {
      await BankInfoApi.createBankInfo(data)
      message.success(t('common.createSuccess'))
    } else {
      await BankInfoApi.updateBankInfo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    type: undefined,
    compId: undefined,
    bankName: undefined,
    swiftCode: undefined,
    bankCode: undefined,
    branchCode: undefined,
    bankAddress: undefined,
    acName: undefined,
    account: undefined,
    defaultStatus: undefined,
    remark: undefined,
    pic: undefined
  }
  formRef.value?.resetFields()
}
</script>
